/*
============================================================================
Name        : BlockTelOrder.cpp
Author      : emoney
Version     :
Copyright   : www.emoney.cn
Description : CBlockTelOrder implementation
============================================================================
*/

#include "BlockTelOrder.h"
#include "Control.h"
#include "eStockappui.h"
#include "eStockMainGridContainer.h"
#include "BlockMemo.h"
#include <eStock_L2hy.rsg>
#include <aknutils.h>
#include <COEUTILS.H>
#include "Logger.h"
#include <stringloader.h>

const TInt KPeriodicTimerInterval50millisecond = 50000;

const TInt KMenuIdAddQS = 100;
const TInt KMenuIdDelQS = 101;
const TInt KMenuIdCallQS = 102;

#ifdef __WINS__
_LIT(KRecordBrokers,"c:\\estock\\brokers.dat");
#else
#ifdef __SERIES60_3X__
_LIT(KRecordBrokers,"\\private\\e8681e80\\brokers.dat");
#else
_LIT(KRecordBrokers,"\\system\\apps\\estock_L2\\brokers.dat");
#endif//__SERIES60_3X__
#endif	//__WINS__

CBlockTelOrder::CBlockTelOrder(CBaseBlock* bsBack,const TInt &nReqType,const TDesC& aDesMain,const TDesC& aDesMinor):
CBlock(bsBack),
m_nReqType(nReqType),
m_strMain(aDesMain),
m_strMinor(aDesMinor),
m_bWantSave(EFalse)
{
	iOffset = 0;
	iHeight = m_nLineHeight;
}

CBlockTelOrder::~CBlockTelOrder()
{
	if(m_nReqType == 0)
		SaveBrokers();

	m_vBroker.Close();
	m_vBrokerMatched.Close();
	
	if(m_AnimationTimer)
		delete m_AnimationTimer, m_AnimationTimer = NULL;
		
	delete m_Input;

	if(m_CallMaker != NULL)
		delete m_CallMaker, m_CallMaker = NULL;
	
	if (iFlickable != NULL)
		delete iFlickable, iFlickable = NULL;
}

CBlockTelOrder* CBlockTelOrder::NewLC(CBaseBlock* bsBack,const TInt &nReqType,const TDesC& aDesMain,const TDesC& aDesMinor)
{
	CBlockTelOrder* self = new (ELeave)CBlockTelOrder(bsBack,nReqType,aDesMain,aDesMinor);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockTelOrder* CBlockTelOrder::NewL(CBaseBlock* bsBack,const TInt &nReqType,const TDesC& aDesMain,const TDesC& aDesMinor)
{
	CBlockTelOrder* self=CBlockTelOrder::NewLC(bsBack,nReqType,aDesMain,aDesMinor);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockTelOrder::ConstructL()
{
	CBlock::ConstructL();

	CGlobal::GetDesFromID(m_strCancel,LOC_BUF_MENU_BACK);

	if(m_strMain.Length() == 0)
	{
		if(m_nReqType == 0)
			CGlobal::GetDesFromID(m_strTitle,LOC_BUF_STR_WDQS);
		else
			CGlobal::GetDesFromID(m_strTitle,LOC_BUF_STR_QSLB);
	}
	else
		m_strTitle = m_strMain;

	m_AnimationTimer = CPeriodic::NewL(CActive::EPriorityIdle);

	if(m_strMinor.Length()>0)
	{
		TInt len = 0;
		m_strTitle.Append(_L("-"));
		len = m_strMinor.Length();
		TBuf<64> tmp(m_strMinor);
		tmp.TrimAll();
		len = tmp.Length();
		m_strTitle.Append(tmp);
	}

	if(m_nReqType == 0)
	{
		LoadBrokers();
		CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_OPTION);
		InitPopMenu(0);
		m_bSocketed = ETrue;

		CheckRoll();
//		SetScrollBar(iHighlight, m_vBrokerMatched.Count());
	}
	else if(m_nReqType == RequestDataType_TelOrderBody)
		CGlobal::GetDesFromID(m_strOK,LOC_BUF_STR_ADD);


	TBuf<6> buf;
	CGlobal::GetDesFromID(buf,LOC_BUF_STR_PYCX);
	m_Input = CSingleLineTextInput::NewL(buf, 10);
	m_Input->SetNumber(EFalse);
	//m_Input->SetActive(ETrue);

	m_bOutofTest = EFalse;

	CGlobal::GetDesFromID(m_bufDownloading,LOC_BUF_STR_DOWNLOADING);
	CGlobal::GetDesFromID(m_bufNobroker,LOC_BUF_STR_NOBROKER);
	CGlobal::GetDesFromID(m_bufAddbroker,LOC_BUF_STR_ADDBROKER);
	CGlobal::GetDesFromID(m_bufNoInfo,LOC_BUF_STR_NOINFO);
	
	iFlickable = CFlickable::NewL();
	iFlickable->SetAcceptMouseClick(this);
	iFlickable->SetThreshold(m_nLineHeight);
}

TPoint CBlockTelOrder::ScrollOffset() const
{
	return TPoint(0, iOffset);
}

void CBlockTelOrder::SetScrollOffset(const TPoint& aOffset)
{
	TInt y = aOffset.iY;
	if (y != iOffset)
	{
		iOffset = Max(0, Min(iHeight * m_vBrokerMatched.Count() - iListRect.Height(), y));
		DrawNow();

		if (m_bShowScroll)
		{
			TInt size = m_vBrokerMatched.Count();
			if (iStartItem == 0)
				m_sbVert->SetPos(iStartItem);
			else if (iEndItem == size - 1)
				m_sbVert->SetPos(iEndItem);
			else
				m_sbVert->SetPos((iStartItem + iEndItem) / 2);
		}
	}
}

void CBlockTelOrder::SizeChanged()
{
	CBlock::SizeChanged();
    m_Input->SetRect(0, m_rect.iBr.iY - m_nLineHeight, m_rect.iBr.iX, m_rect.iBr.iY);
    iListRect = m_rect;
    if (m_nReqType != RequestDataType_WapOrder)
        iListRect.iBr.iY -= (m_Input->m_rect.Height() + 2);
    
    m_sbVert->m_rect.iBr.iY = iListRect.iBr.iY - 2;
    m_sbVert->SizeChanged();
	    
	if(m_nReqType == 0)
	{
		iToolbar->SetSoftKey(CToolbar::EMenu, CToolbar::EBack);
		SetScrollBar(iHighlight, m_vBrokerMatched.Count());
	}
	else if(m_nReqType == RequestDataType_TelOrderBody)
		iToolbar->SetSoftKey(CToolbar::EAdd, CToolbar::EBack);
	else
		iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
	
	if (m_nReqType == RequestDataType_WapOrder)
		iTabbar->SetSelectedId(KTabWangYeWeiTuo);
	else
		iTabbar->SetSelectedId(KTabDianHuaWeiTuo);
}

void CBlockTelOrder::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockTelOrder::OnDraw(CFbsBitGc& g)const
{
	TInt nSize = m_vBrokerMatched.Count();
	if(m_nReqType != RequestDataType_WapOrder)
		m_Input->Draw(g);

	if (!m_bSocketed)
	{
		CGlobal::DrawString(g,m_bufDownloading,m_rect.iTl.iX,m_rect.iTl.iY,TOP|LEFT,CGlobal::Static()->g_rgbText);
		return;
	}
	else if(m_nReqType == 0 && m_vBroker.Count()==0)
	{
		CGlobal::DrawString(g,m_bufNobroker,m_rect.iTl.iX,m_rect.iTl.iY,TOP|LEFT,CGlobal::Static()->g_rgbText);
		CGlobal::DrawString(g,m_bufAddbroker,m_rect.iTl.iX,m_rect.iTl.iY + m_nLineHeight,TOP|LEFT,CGlobal::Static()->g_rgbText);
		return;
	}
	else if (nSize==0)
	{
		CGlobal::DrawString(g,m_bufNoInfo,m_rect.iTl.iX,m_rect.iTl.iY,TOP|LEFT,CGlobal::Static()->g_rgbText);
		return;
	}

	if (nSize>0)
	{
		iStartItem = iOffset / iHeight;
		TInt y = iListRect.iTl.iY + iStartItem * iHeight - iOffset;
		if (iOffset <= 0)
		{
			iStartItem = 0;
			y = iListRect.iTl.iY;
		}
		iEndItem = iStartItem + iListRect.Height() / iHeight + 1;
		if (iEndItem > nSize - 1)
			iEndItem = nSize - 1;

		g.SetClippingRect(iListRect);
		for (TInt i = iStartItem; i<= iEndItem; ++i, y += iHeight)
		{
			TBuf<200> str;
			TBrokerMap broker= m_vBrokerMatched[i];
			str.AppendNum(i+1);
			str.Append(_L("."));
			if (m_nReqType==RequestDataType_WapOrder)
				str.Append(broker.m_strTel);
			else
				str.Append(broker.m_strText);
			//str.Append(broker.m_strText);
			str.TrimAll();
			if(m_nReqType == RequestDataType_TelOrderBody || m_nReqType == 0)
			{
				str.Append(_L("-"));
				str.Append(broker.m_strTel);
			}
			if (m_nReqType==RequestDataType_WapOrder)
			{
				str.Append(_L("-"));
				str.Append(broker.m_strText);
			}
			if (iHighlight == i)
			{
				DrawImageRect(g,0, y, m_rect.Width(), y+m_nLineHeight,1);
				TInt nWidth = CGlobal::GetStringWidth(str);
				if(m_AnimationTimer->IsActive() && m_nX<0)
				{
					if(m_nX < -nWidth)
						m_nX = m_rect.Width();	
				}
			}

			if (iHighlight == i)
				CGlobal::DrawString(g,str,m_nX,y,TOP|LEFT,CGlobal::Static()->g_rgbText);
			else
				CGlobal::DrawString(g,str,0,y,TOP|LEFT,CGlobal::Static()->g_rgbText);
		}
		g.CancelClippingRect();
	}
}

void CBlockTelOrder::OnKeyEvent(const TInt& keyCode)
{
	if(menuBar->IsPopup())
	{
		menuBar->OnKeyDown(keyCode);
		DrawNow();
		return;
	}
	if(m_nReqType==0 && (keyCode==m_pGlobal->g_nKeyOK/* || keyCode==EStdKeyNkpAsterisk ||keyCode=='*' */))
	{
		menuBar->SetPopup(ETrue);
		DrawNow();
		return;
	}
	if(m_Input->Focus())
	{
		if(keyCode ==CGlobal::Static()->g_nKeyUp)
		{
			m_Input->SetFocus(EFalse);
			DrawNow();
		}
		else if(keyCode ==CGlobal::Static()->g_nKeyDown)
		{
			m_Input->SetFocus(EFalse);
			iToolbar->SetFocus(ETrue);
			DrawNow();
		}
		else if(m_Input->OnKeyDown(keyCode) && m_nReqType != RequestDataType_WapOrder)
		{
			TBuf<6> buf;
			m_Input->GetText(buf);
			OnInput(buf);
			CheckRoll();
			DrawNow();
		}
		return;
	}
	if (keyCode==CGlobal::Static()->g_nKeyUp||keyCode==CGlobal::Static()->g_nKeyDown)
	{
		TInt nLine = m_vBrokerMatched.Count();
		if (nLine>=0)
		{
			TInt nOldCurrent = iHighlight;
			if (keyCode==CGlobal::Static()->g_nKeyUp)
			{
				iHighlight--;
				if (iHighlight<0)
				{
					iHighlight = 0;
					iTabbar->SetFocus(ETrue);
					DrawNow();
				}
				else
				{
					if (iHighlight * iHeight < iOffset)
						iOffset -= iHeight;
				}
			}
			else if (keyCode==CGlobal::Static()->g_nKeyDown)
			{
				iHighlight++;
				if (iHighlight >= nLine)
				{
					iHighlight = nLine - 1;
					if (nLine == 0)
					    iHighlight = 0;
					
					if(RequestDataType_WapOrder == m_nReqType)
					{
						iToolbar->SetFocus(ETrue);
					}
					else
					{
						m_Input->SetFocus(ETrue);
					}
					DrawNow();
				}
				else
				{
					if (iOffset + iListRect.Height() < (iHighlight + 1) * iHeight)
						iOffset += iHeight;
				}
			}

			if (nOldCurrent != iHighlight)
			{
				CheckRoll();
				if (m_bShowScroll)
					m_sbVert->SetPos(iHighlight);
				DrawNow();
			}
		}
		return;
	}
	else if (keyCode==CGlobal::Static()->g_nKeySelect || 
		keyCode==CGlobal::Static()->g_nKeyOK/* ||keyCode=='*'*/)
	{
		TInt nLine = m_vBrokerMatched.Count();
		if (iHighlight>=0&&iHighlight<nLine)
		{
			TBrokerMap broker = m_vBrokerMatched[iHighlight];

			if(m_nReqType==RequestDataType_WapOrder)
			{
				CBlockMemo::BrowseWAPPageL(broker.m_strText);
			}
			else
			{
				if (m_nReqType==RequestDataType_TelOrderName)
				{
					GetContainer()->AddBlock(CBlockTelOrder::NewL(this, RequestDataType_TelOrderArea, broker.m_strText, KNullDesC));
				}
				else if (m_nReqType==RequestDataType_TelOrderArea)
				{
					GetContainer()->AddBlock(CBlockTelOrder::NewL(this, RequestDataType_TelOrderBody, m_strMain, broker.m_strText));
				}
				else if (m_nReqType==RequestDataType_TelOrderBody)
				{
					TBuf<16> buf;
					CBlock* block = (CBlock*)m_pBlockBack->m_pBlockBack->m_pBlockBack;
					CBlockTelOrder* blocktelorder = (CBlockTelOrder*)block;
					buf.Copy(m_strMain);
					buf.Append(_L("-"));
					broker.m_strText.Insert(0,buf);
					blocktelorder->AddBroker(broker);

					CGlobal::GetDesFromID(buf,LOC_BUF_STR_BROKEROK);
					GetContainer()->AddBlock(CBlockMemo::NewL(block, 101,buf));
				}
			}
		}
		return;
	}
	if(keyCode==CGlobal::Static()->g_nKeyCancel)
	{
		if(m_vBroker.Count()>0 && (m_vBrokerMatched.Count()<m_vBroker.Count()))
			OnInput(KNullDesC);
		else
		{
			if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
				eStockUI->GetCurView()->iContainer->DelBlock();
		}
		return;
	}
	CBlock::OnKeyEvent(keyCode);
}

void CBlockTelOrder::ChangeFocus(TBool aFoucs, const TInt& keyCode)
{
 	if(aFoucs && keyCode == m_pGlobal->g_nKeyUp
 		 && m_nReqType != RequestDataType_WapOrder)
 		m_Input->SetFocus(ETrue);
}

TBool CBlockTelOrder::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);
	TInt nError = CGlobal::ReadInt32L(ptr);
	HBufC* strError = CGlobal::ReadStringL(ptr);
	if (nError!=0)
	{
		GetContainer()->AddBlock(CBlockMemo::NewL(NULL, 2, strError->Des()));
	}
	else
	{
		TInt nSize = CGlobal::ReadInt32L(ptr);

		m_vBroker.Reset();
		m_vBrokerMatched.Reset();

		for (TInt i = 0; i<nSize; i++)
		{
			HBufC* buf = NULL;
			TInt len = 0;
			TBrokerMap broker;
			buf = CGlobal::ReadStringL(ptr);
			len = buf->Length();
			if (m_nReqType==RequestDataType_WapOrder)
				broker.m_strTel.Copy(buf->Des());
			else
				broker.m_strText.Copy(buf->Des());
			delete buf;
			if(m_nReqType != RequestDataType_WapOrder )
			{
				buf = CGlobal::ReadStringL(ptr);
				len = buf->Length();
				broker.m_strPinyin.Copy(buf->Des());
				delete buf;
			}
			if(m_nReqType==RequestDataType_TelOrderBody)
			{
				buf = CGlobal::ReadStringL(ptr);
				len = buf->Length();
				broker.m_strTel.Copy(buf->Des());
				delete buf;
			}
			if (m_nReqType==RequestDataType_WapOrder)
			{
				buf = CGlobal::ReadStringL(ptr);
				len = buf->Length();
				broker.m_strText.Copy(buf->Des());
				if (broker.m_strText[0] == 'w' || broker.m_strText[0] == 'W')
				{
					broker.m_strText.Insert(0, _L("http://"));
				}
				delete buf;
			}
			m_vBroker.Append(broker);
			m_vBrokerMatched.Append(broker);
		}
		if (iHighlight>=nSize)
			iHighlight = 0;

		CheckRoll();

		SetScrollBar(iHighlight, nSize);
	}
	SizeChanged();
	m_bSocketed = ETrue;
	m_bExit = ETrue;
	delete strError;
	return ETrue;
}

void CBlockTelOrder::WriteData(TDes8& aBuf)
{
	if(m_strMain.Length()>0)
		CGlobal::WriteString(aBuf, m_strMain);
	if(m_strMinor.Length()>0)
		CGlobal::WriteString(aBuf, m_strMinor);
	
	if (m_nReqType == RequestDataType_WapOrder)
		CGlobal::WriteInt16L(aBuf, 2);
}

TInt CBlockTelOrder::GetDataLength()
{
	TInt length = 0;
	if(m_strMain.Length()>0)
		length += CGlobal::GetWriteLength(m_strMain);
	if(m_strMinor.Length()>0)
		length += CGlobal::GetWriteLength(m_strMinor);
	
	if (m_nReqType == RequestDataType_WapOrder)
		length += 2;
	return length;
}

TInt16 CBlockTelOrder::GetDataType()
{
	if(m_bSocketed)
		return RequestDataType_None;
	else
		return m_nReqType;
}

void CBlockTelOrder::ChangeScrollPos(TInt /*nPos*/)
{
//	if (iHighlight!=nPos)
//	{
//		iHighlight = nPos;
//		DrawNow();
//	}
}

void CBlockTelOrder::InitPopMenu(const TInt& /*nType*/)
{
	menuBar->m_subMenu.ResetAndDestroy();

	menuBar->Append(CPopMenu::NewL( KMenuIdAddQS,LOC_BUF_STR_ADDQS));
	menuBar->Append(CPopMenu::NewL( KMenuIdDelQS,LOC_BUF_STR_DELQS));
	menuBar->Append(CPopMenu::NewL( KMenuIdCallQS,LOC_BUF_STR_CALL));
	//menuBar->Append(CPopMenu::NewL( KMenuIdHelp,LOC_BUF_STR_HELP));
}

TBool CBlockTelOrder::OnMenuEvent(TInt menuId)
{
	if (menuId==KMenuIdAddQS)
	{
		GetContainer()->AddBlock(CBlockTelOrder::NewL(this,
			RequestDataType_TelOrderName, KNullDesC, KNullDesC));
	}
	else if (menuId == KMenuIdDelQS)
	{
		m_bWantSave = ETrue;
		TBrokerMap brokCur = m_vBrokerMatched[iHighlight];
		for(TInt i=0; i<m_vBroker.Count(); i++)
		{
			TBrokerMap brok = m_vBroker[i];
			if(brokCur.m_strText.CompareF(brok.m_strText) == 0)
			{
				m_vBroker.Remove(i);
				break;
			}
		}
		m_vBrokerMatched.Remove(iHighlight);
		if(m_vBroker.Count() == 0)
			InitPopMenu(0);
		return ETrue;
	}

	else if(menuId == KMenuIdCallQS)
	{
		MakeCall();
		return ETrue;
	}
	else if(menuId == KMenuIdHelp)
	{
		HBufC* buf = StringLoader::LoadLC(LOC_BUF_STR_QSHELP);
		GetContainer()->AddBlock(CBlockMemo::NewL(this,101,buf->Des()));
		CleanupStack::PopAndDestroy();
		return ETrue;
	}
	return EFalse;
}

void CBlockTelOrder::OnMouseDown(const TPoint& aPt)
{
	iFlickable->HandleMousePress(aPt);
}

void CBlockTelOrder::OnMouseMove(const TPoint& aPt)
{
	iFlickable->HandleMouseMove(aPt);
}

void CBlockTelOrder::OnMouseUp(const TPoint& aPt)
{
	if (iFlickable->HandleMouseRelease(aPt))
		return;
	
	if (iListRect.Contains(aPt))
	{
		int y = aPt.iY - iListRect.iTl.iY + iOffset;
		int i = y / iHeight;
		if (i != iHighlight) 
		{
			iHighlight = i;
			DrawNow();
		}
		else if (i == iHighlight)
			OnKeyEvent(m_pGlobal->g_nKeySelect);
	}
	else if(m_Input->m_rect.Contains(aPt) && m_nReqType != RequestDataType_WapOrder)
	{	
		m_Input->SetFocus(ETrue);
		m_kb->SetInputType(CKeyBoard::LETTER);
		DrawNow();
	}
}

void CBlockTelOrder::OnInput(const TDesC8& strInput)
{
	if (m_kb->m_bInputGoodCode)
	{
		CBlock::OnInput(strInput);
		return;
	}
	
	TBuf<6> buf;
	buf.Copy(strInput);
	OnInput(buf);
}

void CBlockTelOrder::OnInput(const TDesC& strInput)
{
	m_vBrokerMatched.Reset();

	for (TInt i = 0; i<m_vBroker.Count(); i++)
	{
		TBrokerMap broker = m_vBroker[i];
		if (broker.m_strPinyin.Length()>=strInput.Length()&&
			broker.m_strPinyin.FindF(strInput)!=KErrNotFound)
			m_vBrokerMatched.Append(broker);
	}

	TInt nSize = m_vBrokerMatched.Count();
	if (iHighlight>=nSize)
		iHighlight = 0;
	SetScrollBar(iHighlight, nSize);
	m_Input->SetRect(0,m_rect.iBr.iY-m_nLineHeight-1,m_rect.iBr.iX,
		m_rect.iBr.iY-1);
	/*m_Input->SetText(strInput);*/
}

void CBlockTelOrder::AddBroker(const TBrokerMap& aBroker)
{
	for(TInt i=0; i<m_vBroker.Count(); i++)
	{
		TBrokerMap brok = m_vBroker[i];
		if(brok.m_strText.CompareF(aBroker.m_strText) == 0)
			return;
	}
	m_bWantSave = ETrue;
	m_vBroker.Append(aBroker);
	OnInput(KNullDesC);
	//if(m_vBroker.Count() == 1)
	//	InitPopMenu(0);

}

void CBlockTelOrder::CheckRoll()
{
	if(iHighlight>=m_vBrokerMatched.Count())
		return;

	StopRoll();

	TBuf<200> str;
	TBrokerMap broker= m_vBrokerMatched[iHighlight];
	str.AppendNum(iHighlight);
	str.Append(_L("."));
	str.Append(broker.m_strText);
	str.TrimAll();
	if(m_nReqType == RequestDataType_TelOrderBody || m_nReqType == 0)
	{
		str.Append(_L("-"));
		str.Append(broker.m_strTel);
	}
	if(m_nReqType==RequestDataType_WapOrder)
	{
		str.Append(_L("-"));
		str.Append(broker.m_strText);
	}

	TInt nWidth = CGlobal::GetStringWidth(str);
	if (nWidth>m_rect.Width())
	{
		StartRoll();
	}
}

void CBlockTelOrder::CheckEmpty()
{
	if(m_nReqType == 0 && m_vBroker.Count() == 0)
	{
		GetContainer()->AddBlock(CBlockTelOrder::NewL(this,
			RequestDataType_TelOrderName, KNullDesC, KNullDesC));
	}
}

void CBlockTelOrder::StartRoll()
{
	m_AnimationTimer->Start(
		KPeriodicTimerInterval50millisecond, KPeriodicTimerInterval50millisecond, 
		TCallBack(OnRoll, this));
}

void CBlockTelOrder::StopRoll()
{
	m_nX = 0;
	if(m_AnimationTimer->IsActive())
		m_AnimationTimer->Cancel();
}

TInt CBlockTelOrder::OnRoll(TAny* aAny)
{
	CBlockTelOrder* self = static_cast<CBlockTelOrder*>( aAny );
	if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
	{
		if(self != eStockUI->GetCurView()->iContainer->m_block)
		{
			self->m_AnimationTimer->Cancel();
			self->m_nX = 0;
			return KErrNone;
		}
	}
	self->m_nX -= 2;
	self->DrawNow();
	return KErrNone;
}

void CBlockTelOrder::SaveBrokers()
{
	if(!m_bWantSave)
		return;

	TFileName fileName(KRecordBrokers);
	CompleteWithAppPath(fileName);
	ConeUtils::EnsurePathExistsL(fileName);
	CFileStore* filestore = CDirectFileStore::ReplaceLC(CEikonEnv::Static()->FsSession(),fileName,EFileWrite);
	filestore->SetTypeL(KDirectFileStoreLayoutUid);
	RStoreWriteStream stream;
	TStreamId id = stream.CreateLC(*filestore);

	TInt count = m_vBroker.Count();
	stream.WriteInt16L(count);

	for(TInt i=0; i<count; i++)
	{
		TBrokerMap broker = m_vBroker[i];
		stream.WriteInt16L(broker.m_strText.Length());
		stream.WriteL(broker.m_strText);
		stream.WriteInt16L(broker.m_strTel.Length());
		stream.WriteL(broker.m_strTel);
		stream.WriteInt16L(broker.m_strPinyin.Length());
		stream.WriteL(broker.m_strPinyin);
	}

	stream.CommitL();
	CleanupStack::PopAndDestroy();	//stream id
	filestore->SetRootL(id);
	filestore->CommitL();
	CleanupStack::PopAndDestroy();
}

void CBlockTelOrder::LoadBrokers()
{
	TFileName fileName(KRecordBrokers);
	CompleteWithAppPath(fileName);
	if(ConeUtils::FileExists(fileName) )
	{
		CFileStore *readStore = CDirectFileStore::OpenLC(CEikonEnv::Static()->FsSession(),fileName, EFileShareAny|EFileStream|EFileRead);
		RStoreReadStream stream;
		stream.OpenLC(*readStore,readStore->Root());

		TInt count = stream.ReadInt16L();

		for(TInt i=0; i<count; i++)
		{
			TBrokerMap broker;
			TInt length = stream.ReadInt16L();
			stream.ReadL(broker.m_strText,length);
			length = stream.ReadInt16L();
			stream.ReadL(broker.m_strTel,length);
			length = stream.ReadInt16L();
			stream.ReadL(broker.m_strPinyin,length);
			m_vBroker.Append(broker);
			m_vBrokerMatched.Append(broker);
		}

		CleanupStack::PopAndDestroy(2);
	}
}

void CBlockTelOrder::MakeCall()
{
	TBrokerMap broker  = m_vBrokerMatched[iHighlight]; 

	if(m_CallMaker != NULL)
	{
		delete m_CallMaker;
	}
#ifdef __SERIES60_3X__
	m_CallMaker = CPhoneCallMaker2::NewL(*this);
#else
	m_CallMaker = CPhoneCallMaker::NewL(*this);
#endif
	LOGDES16(broker.m_strTel);
	LOGCALL(m_CallMaker->MakeCallL(broker.m_strTel));
}

void CBlockTelOrder::HandleCallHungUpL()
{
	if(m_CallMaker != NULL)
	{
		delete m_CallMaker;
		m_CallMaker = NULL;
	}

	TApaTask task(CCoeEnv::Static()->WsSession());
	task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
	task.BringToForeground();
}

void CBlockTelOrder::AddTabs()
{
	TBuf<8> label;
	CGlobal::GetDesFromID(label,LOC_BUF_STR_PHONEAGENT);
	iTabbar->AppendTab(label, KTabDianHuaWeiTuo);
	CGlobal::GetDesFromID(label,LOC_BUF_STR_WAPAGENT);
	iTabbar->AppendTab(label, KTabWangYeWeiTuo);
}

void CBlockTelOrder::HandleTabbarCommand(TInt aCommand)
{
	CBlockTelOrder* block = NULL;
	switch (aCommand)
	{
	case KTabDianHuaWeiTuo:
		block = CBlockTelOrder::NewL(NULL,0,KNullDesC,KNullDesC);
		break;
	case KTabWangYeWeiTuo:
		block = CBlockTelOrder::NewL(NULL,RequestDataType_WapOrder,KNullDesC,KNullDesC);
		break;
	default:
		break;
	}
	GetContainer()->AddBlock(block);
	block->CheckEmpty();
}
